<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * @author 安鹏飞
 * @date 2017-10-12
 */
class Map extends MY_Controller
{
	public function __construct() {
	        parent::__construct();
	        $this->load->model('M_Quantification');
             }

	public function device_status()
	{

		$floor = $this->input->post('floor');
		// $floor = 2295735;
		$list = $this->db->select('device_id,device_name,device_type_name,is_show,device_status,small_grid_floor,device_x,device_y')
				->from('t_device')
				->join('t_device_type','t_device_type.device_type_id = t_device.device_type_id','left')
				->join('t_small_grid','t_small_grid.small_grid_id = t_device.smile_grid','left')
				->where_in('small_grid_floor',$floor)
				// ->group_by('small_grid_floor')
				->get()
				->result_array();

		foreach($list as $k => $v)
		{
			if($v['is_show'] == 1)
			{
				$eq[$k]['id'] = $v['device_id'];
				$eq[$k]['name'] = $v['device_name'];
				$eq[$k]['type'] = $v['device_type_name'];
				$eq[$k]['floor'] = $v['small_grid_floor'];
				if($v['device_status'] == 1)
				{
					$eq[$k]['status'] = "正常";
				}else if($v['device_status'] == 2)
				{
					$eq[$k]['status'] = "报修";
				}else if($v['device_status'] == 3)
				{
					$eq[$k]['status'] = "维修";
				}else{
					$eq[$k]['status'] = "验收";
				}
				$eq[$k]['latlng'][] = $v['device_y'];
				$eq[$k]['latlng'][] = $v['device_x'];
			}else{
				if($v['device_status'] != 1)
				{
					$eq[$k]['id'] = $v['device_id'];
					$eq[$k]['name'] = $v['device_name'];
					$eq[$k]['type'] = $v['device_type_name'];
					$eq[$k]['floor'] = $v['small_grid_floor'];
					if($v['device_status'] == 2)
					{
						$eq[$k]['status'] = "报修";
					}else if($v['device_status'] == 3)
					{
						$eq[$k]['status'] = "维修";
					}else{
						$eq[$k]['status'] = "验收";
					}
					$eq[$k]['latlng'][] = $v['device_y'];
					$eq[$k]['latlng'][] = $v['device_x'];
				}
			}
			
		}
		if($eq)
		{
			$res['code'] = 0;
			$res['info'] = "获取成功";
			$res['data']['data'] = $eq;
		}else{
			$res['code'] = 1;
			$res['info'] = "获取失败";
		}

		echo json_encode($res);

	}

	/*
		人员（需要打卡记录表）
	 */
	public function  user_status()
	{

		$list = $this->db->select('t_user.user_id,user_name,department_name,workshop_name,post_name,user_job,record_x,record_y,record_floor')
				->from('t_user')
				->join('t_post','t_post.post_id = t_user.post_id','left')
				->join('t_department','t_department.department_id = t_user.department_id','left')
				->join('t_workshop','t_workshop.workshop_id = t_department.workshop_id','left')
				->join('t_user_record','t_user_record.user_id = t_user.user_id','left')
				->where(['is_online' => 1,'is_del' => 1])
				->get()
				->result_array();
		foreach($list as $k => $v)
		{
			$eq[$k]['depName'] = $v['department_name'];
			$eq[$k]['floor'] = $v['record_floor'];
			$eq[$k]['userId'] = $v['user_id'];
			$eq[$k]['userName'] = $v['user_name'];
			$eq[$k]['userPost'] = $v['post_name'];
			$eq[$k]['userJob'] = $v['user_job'];
			$eq[$k]['type'] = $v['workshop_name'];
			$eq[$k]['latlng'][] = $v['record_y'];
			$eq[$k]['latlng'][] = $v['record_x'];
		}
		if($eq)
		{
			$res['code'] = 0;
			$res['info'] = "获取成功";
			$res['data']['data'] = $eq;
		}else{
			$res['code'] = 1;
			$res['info'] = "获取失败";
		}

		echo json_encode($res);

	}
	//量化任务
    	public function get_quantification(){
    		$time=time();
    		//状态status 1 完成 2 接收进行中 3 未接受
    		//两小时
    		$sql = "SELECT sg.small_grid_id,t1.* FROM t_small_grid sg LEFT JOIN (SELECT IF (um.number < um.finish_number,1,2) AS `status`,um.quantification_user_map_addtime AS recieveTime,a.add_time AS endTime,if(a.add_time is null,uus.user_name,u.user_name) AS userName,if(a.add_time is null,pp.post_name,p.post_name) AS userPost,if(a.add_time is null,mm.department_name,d.department_name) AS userDep,um.grid_id FROM t_quantification_user_map um LEFT JOIN t_user uus ON uus.user_id=um.user_id LEFT JOIN t_post pp ON pp.post_id=uus.post_id LEFT JOIN t_department mm ON mm.department_id=uus.department_id LEFT JOIN t_quantification_map m ON um.quantification_map_id = m.quantification_map_id LEFT JOIN t_grid g ON g.grid_id = um.grid_id LEFT JOIN t_quantification_user_map_add a ON a.quantification_user_map_id = um.quantification_user_map_id LEFT JOIN t_user u ON u.user_id = a.user_id LEFT JOIN t_post p ON p.post_id = u.post_id LEFT JOIN t_department d ON d.department_id = u.department_id WHERE g.grid_type_id = 2 AND m.quantification_task_cycle_h = 2 AND m.quantification_task_cycle_d = 0 AND ".$time." >= um.start_time AND ".$time." <= um.end_time GROUP BY um.grid_id) t1 ON sg.grid_id=t1.grid_id WHERE sg.small_grid_name<>'' AND sg.grid_id<>''";
                      	$info = $this->db->query($sql)->result_array();
             	//六小时任务
             	$sql = "SELECT sg.small_grid_id,t1.* FROM t_small_grid sg LEFT JOIN (SELECT IF (um.number < um.finish_number,1,2) AS `status`,um.quantification_user_map_addtime AS recieveTime,a.add_time AS endTime,if(a.add_time is null,uus.user_name,u.user_name) AS userName,if(a.add_time is null,pp.post_name,p.post_name) AS userPost,if(a.add_time is null,mm.department_name,d.department_name) AS userDep,um.grid_id FROM t_quantification_user_map um LEFT JOIN t_user uus ON uus.user_id=um.user_id LEFT JOIN t_post pp ON pp.post_id=uus.post_id LEFT JOIN t_department mm ON mm.department_id=uus.department_id LEFT JOIN t_quantification_map m ON um.quantification_map_id = m.quantification_map_id LEFT JOIN t_grid g ON g.grid_id = um.grid_id LEFT JOIN t_quantification_user_map_add a ON a.quantification_user_map_id = um.quantification_user_map_id LEFT JOIN t_user u ON u.user_id = a.user_id LEFT JOIN t_post p ON p.post_id = u.post_id LEFT JOIN t_department d ON d.department_id = u.department_id WHERE g.grid_type_id = 6 AND m.quantification_task_cycle_h = 6 AND m.quantification_task_cycle_d = 0 AND ".$time." >= um.start_time AND ".$time." <= um.end_time GROUP BY um.grid_id) t1 ON sg.grid_id=t1.grid_id WHERE sg.small_grid_name<>'' AND sg.grid_id<>''";
         		$info1 = $this->db->query($sql)->result_array();
             	// 八小时
                          $sql = "SELECT sg.small_grid_id,t1.* FROM t_small_grid sg LEFT JOIN (SELECT IF (um.number < um.finish_number,1,2) AS `status`,um.quantification_user_map_addtime AS recieveTime,a.add_time AS endTime,if(a.add_time is null,uus.user_name,u.user_name) AS userName,if(a.add_time is null,pp.post_name,p.post_name) AS userPost,if(a.add_time is null,mm.department_name,d.department_name) AS userDep,um.grid_id FROM t_quantification_user_map um LEFT JOIN t_user uus ON uus.user_id=um.user_id LEFT JOIN t_post pp ON pp.post_id=uus.post_id LEFT JOIN t_department mm ON mm.department_id=uus.department_id LEFT JOIN t_quantification_map m ON um.quantification_map_id = m.quantification_map_id LEFT JOIN t_grid g ON g.grid_id = um.grid_id LEFT JOIN t_quantification_user_map_add a ON a.quantification_user_map_id = um.quantification_user_map_id LEFT JOIN t_user u ON u.user_id = a.user_id LEFT JOIN t_post p ON p.post_id = u.post_id LEFT JOIN t_department d ON d.department_id = u.department_id WHERE g.grid_type_id = 8 AND m.quantification_task_cycle_h = 8 AND m.quantification_task_cycle_d = 0 AND ".$time." >= um.start_time AND ".$time." <= um.end_time GROUP BY um.grid_id) t1 ON sg.grid_id=t1.grid_id WHERE sg.small_grid_name<>'' AND sg.grid_id<>''";
         		$info2 = $this->db->query($sql)->result_array();
             	// 十二小时
             	$sql = "SELECT sg.small_grid_id,t1.* FROM t_small_grid sg LEFT JOIN (SELECT IF (um.number < um.finish_number,1,2) AS `status`,um.quantification_user_map_addtime AS recieveTime,a.add_time AS endTime,if(a.add_time is null,uus.user_name,u.user_name) AS userName,if(a.add_time is null,pp.post_name,p.post_name) AS userPost,if(a.add_time is null,mm.department_name,d.department_name) AS userDep,um.grid_id FROM t_quantification_user_map um LEFT JOIN t_user uus ON uus.user_id=um.user_id LEFT JOIN t_post pp ON pp.post_id=uus.post_id LEFT JOIN t_department mm ON mm.department_id=uus.department_id LEFT JOIN t_quantification_map m ON um.quantification_map_id = m.quantification_map_id LEFT JOIN t_grid g ON g.grid_id = um.grid_id LEFT JOIN t_quantification_user_map_add a ON a.quantification_user_map_id = um.quantification_user_map_id LEFT JOIN t_user u ON u.user_id = a.user_id LEFT JOIN t_post p ON p.post_id = u.post_id LEFT JOIN t_department d ON d.department_id = u.department_id WHERE g.grid_type_id = 12 AND m.quantification_task_cycle_h = 12 AND m.quantification_task_cycle_d = 0 AND ".$time." >= um.start_time AND ".$time." <= um.end_time GROUP BY um.grid_id) t1 ON sg.grid_id=t1.grid_id WHERE sg.small_grid_name<>'' AND sg.grid_id<>''";
         		$info3 = $this->M_Quantification->querylist($sql);
         		$data = array_merge($info,$info1,$info2,$info3);
         		foreach ($data as $key => $value) {
             		$list[] = array(
                               		'id'=>$value['small_grid_id'],
                               		'status'=>empty($value['status'])?3:$value['status'],
                                		'areaData'=>array('0'=>array(
                                                                 'userName'=>isset($value['userName'])?$value['userName']:"",
                                                                 'userPost'=>isset($value['userName'])?$value['userPost']:"",
                                                                 'userDep'=>isset($value['userName'])?$value['userDep']:"",
                                                                 'recieveTime'=>isset($value['recieveTime'])?date("Y-m-d H:i:s",$value['recieveTime']):"",
                                                                 'endTime'=>isset($value['endTime'])?date("Y-m-d H:i:s",$value['endTime']):"",
                                                                 )
                                                	),
                		);
         		}
          		$data1 = array(
            			'data'=>$list,
            			'code'=>0,
           			'info'=>'成功',
        		);
        		exit(json_encode($data1));
    	}

}